package tree;

/**
 * 广度优先
 */

import javax.swing.*;
import java.util.*;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.SynchronousQueue;

/**
 * Definition for a binary tree node.
 */
class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;

    TreeNode(int x) {
        val = x;
    }

    TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}

public class BFS {

    public static void main(String[] args) {
        TreeNode node = new TreeNode(5);
        TreeNode node1 = new TreeNode(6);
        node.left = node1;
        TreeNode node2 = new TreeNode(4);
        node.right = node2;
        TreeNode node3 = new TreeNode(7);
        TreeNode node4 = new TreeNode(3);
        node3.right = node4;
        TreeNode node5 = new TreeNode(8);
        node2.left = node5;
        TreeNode node6 = new TreeNode(2);
        node2.right = node6;

        levelOrder3(node);
    }

    /**
     * 二叉树的深度遍历
     *
     * @param root
     * @return
     */
    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> res = new LinkedList<>();
        if (root == null) {
            return res;
        }
        recusion(root, res);
        return res;
    }

    /**
     * 递归的方式进行深度遍历
     *
     * @param root
     * @param res
     */
    private void recusion(TreeNode root, List<Integer> res) {
        if (root == null) {
            return;
        }
        //前序遍历
        res.add(root.val);
        if (root.left != null) {
            recusion(root.left, res);
        }
        // 中序遍历
        // res.add(root.val);
        if (root.right != null) {
            recusion(root.right, res);
        }
        // 后序序遍历
        // res.add(root.val);
    }


    // 前序遍历：根→左→右 根节点先入栈，弹出时访问，先压右子树再压左子树。
    public void preorderTraversal(TreeNode root) {
       Stack<TreeNode> stack = new Stack<>();
       stack.push(root);
       while (!stack.isEmpty()) {
           TreeNode curr = stack.pop();
           System.out.println(curr.val);
           if (curr.right != null) {
               stack.push(curr.right);
           }
           if (curr.left != null) {
               stack.push(curr.left);
           }

       }
    }


    /**
     * 用迭代的方式进行深度遍历，中序遍历
     *
     * @param root
     */
    private List<Integer> inorderTraversal1(TreeNode root) {
        List<Integer> res = new LinkedList<>();
        Stack<TreeNode> stk = new Stack<>();
        while (root != null || !stk.isEmpty()) {
            while (root != null) {
                stk.add(root);
                res.add(root.val);
                root = root.left;
            }
            TreeNode pop = stk.pop();
            res.add(pop.val);
            root = pop.right;
        }
        return res;
    }


    /**
     * 层级遍历,并且输出到链表
     *
     * @param root
     */
    private List<Integer> levelOrder2(TreeNode root) {
        List<Integer> res = new LinkedList<>();
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()) {
            TreeNode node = queue.poll();
            System.out.println(node.val);
            res.add(node.val);
            if (node.right != null) {
                queue.offer(node.right);
            }
            if (node.left != null) {
                queue.offer(node.left);
            }
        }

        return res;
    }

    /**
     * 分层打印
     *
     * @param root
     * @return
     */
    public List<List<Integer>> levelOrder1(TreeNode root) {
        List<List<Integer>> list = new ArrayList<>();
        Queue<TreeNode> queue = new LinkedList<>();
        if (root == null)
            return list;
        queue.add(root);
        while (!queue.isEmpty()) {
            List<Integer> tmp = new ArrayList<>();
            int size = queue.size();
            for (int i = 0; i < size; i++) {
                TreeNode node = queue.poll();
                tmp.add(node.val);
                if (node.left != null)
                    queue.add(node.left);
                if (node.right != null)
                    queue.add(node.right);
            }
            list.add(tmp);
        }
        return list;
    }

    /**
     * 层级遍历
     *
     * @param root
     * @return
     */
    public  static void levelOrder3(TreeNode root) {
    
        
        //无界队列
        LinkedBlockingQueue<TreeNode> queue = new LinkedBlockingQueue<>();
        //有界队列
        ArrayBlockingQueue<TreeNode> queue2 = new ArrayBlockingQueue<>(10);
        //同步移交队列
        SynchronousQueue<TreeNode> queue3 = new SynchronousQueue<>();

        //优先级队列
        PriorityQueue<TreeNode> pquueQueue = new PriorityQueue<>();

        if(root==null) {
            return;
        }
        queue.offer(root);
        while(!queue.isEmpty()){
            int size = queue.size();
            for(int i=0;i<size;i++){
                TreeNode p = queue.poll();
                System.out.println(p.val);
                if (p.left!=null) {
                    queue.offer(p.left);
                }
                if (p.right!=null) {
                 queue.offer(p.right);
                }
            }
        }
    }
}
